<?php
    namespace Home\Controller;
    use Think\Controller;
    class RenyuantiaozhengController extends Controller {
    
    public function _initialize()
    {
        header('Access-Control-Allow-Origin: *');  

        header('Access-Control-Allow-Credentials: true'); 
      
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS'); //允许的请求类型
        
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); 

    }

    public function get_list()
    {
        $page       = I("get.page");
        $limit      = I("get.limit");
        $name       = I("get.name");
        $dept_code  = I("get.dept_code");
        $st_date    = I("get.st_date");
        if(empty($st_date))
        {
            $st_date = 0;
        }
        else
        {
            $st_date = strtotime($st_date);
        }
        

        $where = " 1 = 1 ";

        if(!empty($name))
        {
            $where .= " and name like '%$name%'";
        }

        if(!empty($dept_code))
        {
            $where .= " and dept_code = '$dept_code'";
        }

        $data = M("rlzy.new_staff_info")->where($where)->limit(($page-1)*$limit,$limit)->select();
        $sys_dept_attr_dict_list = M("comm.sys_dept_attr_dict")->select();
        if($st_date != 0)
        {
            $set_checkbonusdays_list = M('performance.set_checkbonusdays')->where("st_date = $st_date")->select();
        }
        

        for ($i=0; $i < count($data); $i++) 
        { 
            $info = $data[$i];

            for ($j=0; $j < count($sys_dept_attr_dict_list); $j++) 
            { 
                if($info['dept_type'] == $sys_dept_attr_dict_list[$j]['id'])
                { 
                    $data[$i]['dept_type'] = $sys_dept_attr_dict_list[$j]['dept_type'];
                }
            }

            for ($j=0; $j < count($set_checkbonusdays_list); $j++) 
            { 
                if($info['dept_code'] == $set_checkbonusdays_list[$j]['dept_code'] && $info['user_id'] == $set_checkbonusdays_list[$j]['user_id'] )
                { 
                     $data[$i]['LAY_CHECKED'] = true;
                }
            }


        }
        $json["data"] = $data;
        $json["code"] = 0;
        $json["count"] = M("rlzy.new_staff_info")->where($where)->count();
        $json["msg"] = "";
        
        $this->ajaxReturn($json,"json");


    }

    public function get_one()
    {
        $uuid = I("post.uuid");

        $info = M("rlzy.new_staff_info")->where("uuid = '$uuid'")->find();
        $info["dept_date"] = date("Y-m-d H:i:s",$info["dept_date"]);
        $json["data"]   = $info;
        $json["code"]   = 0;
        $json["msg"]    = "";

        $this->ajaxReturn($json,"json");
    }

    public function save_one()
    {
        $data = I("post.");

        $where["st_date"] = $data['st_date'];
        $where["dept_code"] = $data['dept_code'];
        $where["user_id"] = $data['user_id'];

        $d['isbonus'] = $data['isbonus'];

        $result = M("performance.set_checkbonusdays")->where($where)->save($d);

        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "修改成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "修改失败";
        }
 
        $this->ajaxReturn($json,"json");
    }

    public function add_one()
    {
        $data = I("post.");
        $uuid = uuid();
        
        $result = M("rlzy.new_staff_info")->add($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "添加成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "添加失败";
        }
 
        $this->ajaxReturn($json,"json");
    }


    public function get_sys_dept_dict()
    {
        // echo 1;
        $data = M("comm.sys_dept_dict")->select();
        for ($i=0; $i < count($data); $i++) 
        { 
            $info = $data[$i];
        }
        $json["data"] = $data;
        $json["code"] = 0;
        $json["count"] =0 ;
        $json["msg"] = "";
        
        $this->ajaxReturn($json,"json");
    }

    public function change_staff_info()
    {
        $uuid       = I('post.uuid');
        $name       = I('post.name');
        $change_date  = I('post.change_date');
        $dept_code  = I('post.dept_code');

        $new_dept_info  = M("comm.sys_dept_dict")->where("dept_code = '$dept_code'")->find();
        $new_dept_name  = $new_dept_info['dept_name'];
        $new_dept_type  = $new_dept_info['dept_type'];

        $new_staff_info = M("rlzy.new_staff_info")->where("uuid = '$uuid'")->find();

        $old_dept_name  =   $new_staff_info['dept_name'];
        $old_dept_type  =   $new_staff_info['dept_type'];
        $user_id        =   $new_staff_info['user_id'];

        $change_data['uuid'] = uuid();
        $change_data['change_date'] = strtotime($change_date);
        $change_data['name'] = $name;
        $change_data['user_id'] = $user_id;
        $change_data['out_dept'] = $old_dept_name;
        $change_data['into_dept'] = $new_dept_name;

        $result1 = M('rlzy.change_dept_info')->add($change_data);

        $data['dept_name'] = $new_dept_name;
        $data['dept_type'] = $new_dept_type;
        $data['dept_code'] = $dept_code;
        $result2 = M('rlzy.new_staff_info')->where("uuid = '$uuid'")->save($data);

        if($result1 !== false && $result2 !== false )
        {
            $json["code"] = 0;
            $json["msg"] = "添加成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "添加失败";
        }
        $this->ajaxReturn($json,"json");
    }


    public function get_change_dept_info_list()
    {
        $page = I("get.page");
        $limit = I("get.limit");
        $name = I("get.name");
        $change_date = I("get.change_date");

        $where = " 1 = 1 ";

        if(!empty($name))
        {
            $where .= " and name like '%$name%'";
        }

        if(!empty($change_date))
        {
            $change_date = strtotime($change_date);
            $where .= " and change_date = '$change_date'";
        }


        $data = M("rlzy.change_dept_info")->where($where)->limit(($page-1)*$limit,$limit)->select();
        for ($i=0; $i < count($data); $i++) 
        { 
            $info = $data[$i];
            $data[$i]['change_date'] = date('Y-m-d',$data[$i]['change_date']);
        }
        $json["data"] = $data;
        $json["code"] = 0;
        $json["count"] = M("rlzy.change_dept_info")->where($where)->count();
        $json["msg"] = "";
        
        $this->ajaxReturn($json,"json");
    }


        public function save_staff_info()
    {
        $st_date            = I("post.st_date");
        $dept_code_list     = I("post.dept_code_list");
        $dept_name_list     = I("post.dept_name_list");
        $user_id_list       = I("post.user_id_list");
        $name_list          = I("post.name_list");

        $dept_code_list     = rtrim($dept_code_list,",");
        $dept_name_list     = rtrim($dept_name_list,",");
        $user_id_list       = rtrim($user_id_list,",");
        $name_list          = rtrim($name_list,",");

        $dept_code_list     = explode(",",$dept_code_list);
        $dept_name_list     = explode(",",$dept_name_list);
        $user_id_list       = explode(",",$user_id_list);
        $name_list          = explode(",",$name_list);

        $st_date = strtotime($st_date);

        $all_count = 0;
        $success_count = 0;
        $fail_count = 0;
        $re_count = 0;

        for ($i=0; $i < count($dept_code_list); $i++) 
        { 
            $data['st_date'] = $st_date;

            $data['dept_code'] = $dept_code_list[$i];
            $data['dept_name'] = $dept_name_list[$i];
            $data['user_id']   = $user_id_list[$i];
            $data['staff_name']= $name_list[$i];

            $info = M('performance.set_checkbonusdays')->where($data)->find();
            if(empty($info))
            {
                $result = M("performance.set_checkbonusdays")->add($data);
                if($result !== false)
                {
                    $success_count++;
                }
                else
                {
                    $fail_count++;
                }
            }
            else
            {
                $re_count++;
            }
            

            
            $all_count++;

        }

        $json["data"] = null;
        $json["code"] = 0;
        $json["count"] = 0;
        $json["msg"] = "共".$all_count."条数据,插入".$success_count."条,重复".$re_count."条,失败".$fail_count."条";
        
        $this->ajaxReturn($json,"json");

    }

}